package com.deke.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deke.entity.AclUser;
import com.deke.security.utils.WebUtils;
import com.deke.system.dao.AclRoleDao;
import com.deke.system.dao.AclUserDao;
import com.deke.system.service.AclUserService;
import com.deke.system.vo.UserVo;
import com.deke.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * @author ：Username 刘亦辰（59372312@qq.com）
 * @date ：Created in 2022/12/1 18:36
 * @description：
 * @modified By：
 * @version:
 **/
@Service
public class AclUserServiceImpl implements AclUserService {

    @Autowired
    private AclUserDao userDao;

    @Autowired
    private AclRoleDao roleDao;
    @Override
    public AclUser queryByUsername(String username) {
        QueryWrapper<AclUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        wrapper.eq("is_deleted", 0);
        AclUser aclUser = userDao.selectOne(wrapper);
        if (ObjectUtil.isNotNull(aclUser)){
            return aclUser;
        }
        return null;
    }
    
    /**
     * @create by: Username 刘亦辰 
     * @description: 获取用户信息(头部导航) 
     * @create time: 2022/12/4 0:32
     * @param null 
     * @return :  
     **/
    @Override
    public Result<AclUser> getUserinfo() {
        QueryWrapper<AclUser> wrapper = new QueryWrapper<>();
        String username = WebUtils.getUsername();
        wrapper.eq("username",username);
        AclUser aclUser = userDao.selectOne(wrapper);
        return new Result<>(2000,"获取用户信息",aclUser);
    }
    
    
    /**
     * @create by: Username 刘亦辰 
     * @description: 查询所有用户 
     * @create time: 2022/12/4 0:34
     * @param null 
     * @return :  
     **/
    @Override
    public Result<IPage<AclUser>> queryByConditionPage(Integer current, Integer pageSize, UserVo userVo ){
        IPage<AclUser> page = new Page(current,pageSize);
        QueryWrapper<AclUser> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(userVo.getUsername())){
            wrapper.like("username",userVo.getUsername());
        }
        if (StringUtils.hasText(userVo.getStartDate())){
            wrapper.ge("gmt_create",userVo.getStartDate());
        }
        if (StringUtils.hasText(userVo.getEndDate())){
            wrapper.le("gmt_create",userVo.getEndDate());
        }
        IPage<AclUser> iPage = userDao.selectPage(page, wrapper);
        return new Result<>(2000,"查寻成功",iPage);
    }

    /**
     * @create by: Username 刘亦辰 
     * @description: 添加用户 
     * @create time: 2022/12/4 1:20
     * @param null
     * @return :  
     **/
    @Override
    public Result addUserByUser(UserVo userVo) {
        AclUser aclUser = new AclUser();
        aclUser.setUsername(userVo.getUsername());
        aclUser.setNickName(userVo.getNickName());
        aclUser.setGmtCreate(LocalDateTime.now());
        aclUser.setGmtModified(LocalDateTime.now());
        int insert = userDao.insert(aclUser);
        if (insert>0){
            return new Result(2000,"添加成功");
        }
        return null;
    }

    /**
     * @create by: Username 刘亦辰 
     * @description: 用户状态更新 
     * @create time: 2022/12/4 18:17
     * @param null
     * @return :  
     **/
    @Override
    public Result updateUserStatusByid(String id, Integer deleted) {
        AclUser aclUser = new AclUser();
        aclUser.setId(id);
        aclUser.setIsDeleted(deleted);
        Integer update = userDao.updateById(aclUser);
        if (update>0){
            return new Result(2000,"更改成功");
        }
        return new Result(0000,"更改失败");
    }

    /**
     * @create by: Username 刘亦辰 
     * @description: 编辑 
     * @create time: 2022/12/4 18:55
     * @param null
     * @return :  
     **/
    @Override
    public Result updateUserByUser(AclUser aclUser) {
        int update = userDao.updateById(aclUser);
        if (update>0){
            return new Result<>(2000,"编辑成功");
        }

        return new Result(0000,"修改失败");
    }

    /**
     * @create by: Username 刘亦辰 
     * @description: 通过用户id 删除 
     * @create time: 2022/12/5 13:47
     * @param null
     * @return :  
     **/
    @Transactional
    @Override
    public Result deleteUserById(String id) {
        //删除用户前，把用户具有的权限一并删除
        roleDao.deleteByUserId(id);
        AclUser aclUser = new AclUser();
        aclUser.setId(id);
        int i = userDao.deleteById(aclUser);
        if (i>0){
          return   new Result<>(2000,"成功");
        }
        return new Result(0000,"失败");
    }


}